import { defineConfig } from "rollup";
import path from "node:path";

import alias from "@rollup/plugin-alias";
import babel from "@rollup/plugin-babel";
import json from "@rollup/plugin-json";
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import terser from "@rollup/plugin-terser";
import typescript from "@rollup/plugin-typescript";
import nodePolyfills from "rollup-plugin-polyfill-node";
// import pkg from "./package.json" assert { type: "json" }   // ok
// rollup-plugin-copy
/**
 * 输出公共部分
 */
const outputCommon = {
    banner: "",
    compact: false,
    sourcemap: true, // boolean | 'inline' | 'hidden'
    sourcemapExcludeSources: true, // 源代码是否添加到 sourcemap 文件中
    footer: "",
};

const externals = [
    //
    "@rollup/plugin-alias",
    "@rollup/plugin-babel",
    "@rollup/plugin-commonjs",
    "@rollup/plugin-json",
    "@rollup/plugin-node-resolve",
    "@rollup/plugin-replace",
    "@rollup/plugin-terser",
    "@rollup/plugin-typescript",
    "rollup-plugin-copy",
    "rollup-plugin-polyfill-node",
];
const globals = {
    "@rollup/plugin-alias": "alias",
    "@rollup/plugin-babel": "babel",
    "@rollup/plugin-commonjs": "commonjs",
    "@rollup/plugin-json": "json",
    "@rollup/plugin-node-resolve": "nodeResolve",
    "@rollup/plugin-replace": "replace",
    "@rollup/plugin-terser": "terser",
    "@rollup/plugin-typescript": "typescript",
    "rollup-plugin-copy": "copy",
    "rollup-plugin-polyfill-node": "nodePolyfills",
};

const _defineConfig = {
    input: ["./src/index.ts"],
    output: [
        {
            // AMD
            format: "amd",
            file: "./dist/index.amd.js",
            name: `amd.namespace`,
            compact: true,
            globals: globals,
            ...outputCommon,
        },
        {
            // browser = CommonJS
            format: "cjs",
            file: "./dist/index.cjs.js",
            name: `cjs.namespace`,
            globals: globals,
            ...outputCommon,
        },
        {
            // node = ES Model
            format: "esm",
            file: "./dist/index.esm.js",
            name: `esm.namespace`,
            globals: globals,
            ...outputCommon,
        },
        {
            // UMD
            format: "umd",
            file: "./dist/index.umd.js",
            name: `umd.namespace`, // umd & iife must be have name
            globals: globals,
            ...outputCommon,
        },
        {
            // IIFE
            format: "iife",
            file: "./dist/index.iife.js",
            name: `iife.namespace`, // umd & iife must be have name
            globals: globals,
            ...outputCommon,
        },
        {
            // SYSTEM
            format: "system",
            file: "./dist/index.system.js",
            name: `system.namespace`,
            globals: globals,
            ...outputCommon,
        },
        {
            // main
            format: "esm",
            file: "./dist/index.js",
            name: `esm.namespace`,
            globals: globals,
            ...outputCommon,
        },
    ],
    plugins: [
        json(),
        commonjs({ include: "**/node_modules/**" }),
        typescript({
            cacheDir: "cache",
            outputToFilesystem: true,
            tsconfig: path.resolve(".", "tsconfig.json"),
        }),
    ],
    // 用来指定代码执行环境的参数，解决this执行undefined问题
    context: "window",
    // 指定外部模块
    external: externals,
};

export default defineConfig(_defineConfig);
